home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / MultiAnimation / Tiny.h < prev   
Encoding:
C/C++ Source or Header  |  2004-09-27  |  7.3 KB  |  173 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Tiny.h
  3. //
  4. // Desc: Header file for the CTiny class.  Its declaraction is found here.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved
  7. //-----------------------------------------------------------------------------
  8.  
  9.  
  10. #ifndef TINY_H
  11. #define TINY_H
  12.  
  13.  
  14. #define IDLE_TRANSITION_TIME 0.125f
  15. #define MOVE_TRANSITION_TIME 0.25f
  16.  
  17.  
  18. typedef std::basic_string< TCHAR > String;
  19.  
  20.  
  21.  
  22.  
  23. #define ANIMINDEX_FAIL      0xffffffff
  24. #define FOOTFALLSOUND00     L"footfall00.wav"
  25. #define FOOTFALLSOUND01     L"footfall01.wav"
  26.  
  27.  
  28. extern CSound * g_apSoundsTiny[ 2 ];
  29.  
  30.  
  31.  
  32.  
  33. // Callback data for pass to the callback handler
  34. struct CallbackDataTiny
  35. {
  36.     DWORD           m_dwFoot;       // Identify which foot caused the callback
  37.     D3DXVECTOR3 *   m_pvCameraPos;  // Camera position when callback happened
  38.     D3DXVECTOR3 *   m_pvTinyPos;    // Tiny's position when callback happened
  39. };
  40.  
  41.  
  42.  
  43.  
  44. //-----------------------------------------------------------------------------
  45. // Name: class CBHandlerTiny
  46. // Desc: Derived from ID3DXAnimationCallbackHandler.  Callback handler for
  47. //       CTiny -- plays the footstep sounds
  48. //-----------------------------------------------------------------------------
  49. class CBHandlerTiny : public ID3DXAnimationCallbackHandler
  50. {
  51.     HRESULT CALLBACK HandleCallback( THIS_ UINT Track, LPVOID pCallbackData )
  52.     {
  53.         CallbackDataTiny * pCD = (CallbackDataTiny *) pCallbackData;
  54.  
  55.         // this is set to NULL if we're not playing sounds
  56.         if( /*fornow*/ ! pCD || ! pCD->m_pvCameraPos )
  57.             return S_OK;
  58.  
  59.         // scale volume by distance from tiny
  60.         D3DXVECTOR3 vDiff;
  61.         D3DXVec3Subtract( & vDiff, pCD->m_pvCameraPos, pCD->m_pvTinyPos );
  62.         float fVolume = min( D3DXVec3LengthSq( & vDiff ), 1.f );
  63.         fVolume *= -3000.f;
  64.  
  65.         // play the sound
  66.         if( pCD && g_apSoundsTiny[ pCD->m_dwFoot ] )
  67.             g_apSoundsTiny[ pCD->m_dwFoot ]->Play( 0, 0, (LONG) fVolume );
  68.  
  69.         return S_OK;
  70.     }
  71. };
  72.  
  73.  
  74.  
  75.  
  76. //-----------------------------------------------------------------------------
  77. // Name: class CTiny
  78. // Desc: This is the character class. It handles character behaviors and the
  79. //       the associated animations.
  80. //-----------------------------------------------------------------------------
  81. class CTiny
  82. {
  83.  
  84. protected:
  85.  
  86.     // -- data structuring
  87.     CMultiAnim *         m_pMA;               // pointer to mesh-type-specific object
  88.     DWORD                m_dwMultiAnimIdx;    // index identifying which CAnimInstance this object uses
  89.     CAnimInstance *      m_pAI;               // pointer to CAnimInstance specific to this object
  90.     std::vector<CTiny*> *m_pv_pChars;         // pointer to global array of CTiny* s
  91.     CSoundManager *      m_pSM;               // pointer to sound management interface
  92.     DWORD                m_dwAnimIdxLoiter,   // Indexes of various animation sets
  93.                          m_dwAnimIdxWalk,
  94.                          m_dwAnimIdxJog;
  95.     CallbackDataTiny     m_CallbackData[ 2 ]; // Data to pass to callback handler
  96.  
  97.     // operational status
  98.     double               m_dTimePrev;         // global time value before last update
  99.     double               m_dTimeCurrent;      // current global time value
  100.     bool                 m_bUserControl;      // true == user is controling character with the keyboard
  101.     bool                 m_bPlaySounds;       // true == this instance is playing sounds
  102.     DWORD                m_dwCurrentTrack;    // current animation track for primary animation
  103.  
  104.     // character traits
  105.     float                m_fSpeed;            // character's movement speed -- in units/second
  106.     float                m_fSpeedTurn;        // character's turning speed -- in radians/second
  107.     ID3DXAnimationCallbackHandler *
  108.                          m_pCallbackHandler;  // pointer to callback inteface to handle callback keys
  109.     D3DXMATRIX           m_mxOrientation;     // transform that gets the mesh into a common world space
  110.     float                m_fPersonalRadius;   // personal space radius -- things can't get closer than this
  111.                                               // (note that no height information is given--not necessary for this sample)
  112.     // character status
  113.     D3DXVECTOR3          m_vPos;              // current position in the map -- in our sample, y is always == 0
  114.     float                m_fFacing;           // current direction the character is facing -- in our sample, it's 2D only
  115.     D3DXVECTOR3          m_vPosTarget;        // This indicates where we are moving to
  116.     float                m_fFacingTarget;     // The direction from Tiny's current position to the final destination
  117.     float                m_fSpeedWalk;
  118.     float                m_fSpeedJog;
  119.     bool                 m_bIdle;             // set when Tiny is idling -- not turning toward a target
  120.     bool                 m_bWaiting;          // set when Tiny is not idle, but cannot move
  121.     double               m_dTimeIdling;       // countdown - Tiny Idles til this goes < 0
  122.     double               m_dTimeWaiting;      // countdown - Tiny is waiting til this goes < 0, then picks a new target
  123.     double               m_dTimeBlocked;      // countdown - Tiny must wait a small time when encountering a blocker before starting to walk again
  124.  
  125.     char                 m_szASName[64];      // Current track's animation set name (for preserving across device reset)
  126.  
  127. public:
  128.  
  129.     CTiny();
  130.     virtual ~CTiny();
  131.     virtual HRESULT Setup( CMultiAnim *pMA, std::vector< CTiny* > *pv_pChars, CSoundManager *pSM, double dTimeCurrent );
  132.     virtual void Cleanup();
  133.     virtual CAnimInstance *GetAnimInstance();
  134.     void GetPosition( D3DXVECTOR3 *pV );
  135.     void GetFacing( D3DXVECTOR3 *pV );
  136.     virtual void Animate( double dTimeDelta );
  137.     virtual HRESULT ResetTime();
  138.     virtual HRESULT AdvanceTime( double dTimeDelta, D3DXVECTOR3 *pvEye );
  139.     virtual HRESULT Draw();
  140.     virtual void Report( std::vector < String > & v_sReport );
  141.     virtual void SetUserControl();
  142.     virtual void SetAutoControl();
  143.     virtual bool IsUserControl() { return m_bUserControl; }
  144.     virtual void SetSounds( bool bSounds );
  145.     virtual void ChooseNewLocation( D3DXVECTOR3 *pV );
  146.  
  147.     HRESULT RestoreDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice );
  148.     HRESULT InvalidateDeviceObjects();
  149.  
  150. protected:      // ************ The following are not callable by the app -- internal stuff
  151.  
  152.     bool IsBlockedByCharacter( D3DXVECTOR3 *pV );
  153.     DWORD GetAnimIndex( char sString[] );
  154.     HRESULT AddCallbackKeysAndCompress( LPD3DXANIMATIONCONTROLLER pAC, LPD3DXKEYFRAMEDANIMATIONSET pAS, DWORD dwNumCallbackKeys, D3DXKEY_CALLBACK aKeys[], DWORD dwCompressionFlags, FLOAT fCompression );
  155.     HRESULT SetupCallbacksAndCompression();
  156.     void SmoothLoiter();
  157.     void SetNewTarget();
  158.     double GetSpeedScale();
  159.     void AnimateUserControl( double dTimeDelta );
  160.     void AnimateIdle( double dTimeDelta );
  161.     void AnimateMoving( double dTimeDelta );
  162.     void ComputeFacingTarget();
  163.     void SetIdleState();
  164.     void SetSeekingState();
  165.     void SetMoveKey();
  166.     void SetIdleKey( bool bResetPosition );
  167.     virtual bool IsOutOfBounds( D3DXVECTOR3 *pV );
  168. };
  169.  
  170.  
  171.  
  172. #endif // #ifndef TINY_H
  173.